package homework;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @classname:demo04
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/10 20:23
 * @version: 0.1
 * @since:1.8
 */
public class demo04 {
    public static void main(String[] args) {
        String lastDay = getLastDayOfMonth(2020,12,1);
        System.out.println("获取当前月的最后一天：" + lastDay);
    }

    public static String getLastDayOfMonth(int year, int month,int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DATE,day);
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        System.out.println(lastDay);
        cal.set(Calendar.DAY_OF_MONTH, lastDay);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth;
    }
}
